package com.wuhao.code.check.style.arrangement.kotlin

import com.intellij.util.containers.ContainerUtil
import org.jetbrains.kotlin.psi.KtProperty

/**
 * @author 吴昊
 * @since 1.3.5
 */
class PropertyDependenciesManager(private val myFieldDependencies: Map<KtProperty, Set<KtProperty>>,
                                  fields: Map<KtProperty, KotlinElementArrangementEntry>) {

  val roots: List<KotlinArrangementEntryDependencyInfo>
    get() {
      val list = ContainerUtil.newArrayList<KotlinArrangementEntryDependencyInfo>()

      for ((key, value) in myFieldDependencies) {
        val currentInfo = myFieldInfosMap[key]

        for (usedInInitialization in value) {
          val fieldInfo = myFieldInfosMap[usedInInitialization]
          if (fieldInfo != null && currentInfo != null) {
            currentInfo.addDependentEntryInfo(fieldInfo)
          }
        }

        list.add(currentInfo)
      }

      return list
    }
  private val myFieldInfosMap = ContainerUtil.newHashMap<KtProperty, KotlinArrangementEntryDependencyInfo>()

  init {
    for (field in fields.keys) {
      val entry = fields[field]
      if (entry != null) {
        myFieldInfosMap[field] = KotlinArrangementEntryDependencyInfo(entry)
      }
    }
  }

}

